package com.android.killtencent.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.killtencent.BaseApp;
import com.android.killtencent.R;
import com.android.killtencent.fragment.NewsFatherFragment;
import com.android.killtencent.utils.AppManager;
import com.android.pc.ioc.inject.InjectInit;
import com.android.pc.ioc.inject.InjectLayer;
import com.android.pc.ioc.inject.InjectView;

/**
 * 作者:	Rose
 * 业务名:   主界面
 * 功能说明: 主界面逻辑处理
 * 编写日期:    2015-02-11
 */
@InjectLayer(value = R.layout.activity_main_layout, isTitle = true)
public class MainFramworkActivity extends ActionBarActivity {
    private Context mContext;
    @InjectView
    private LinearLayout main_rgs;
    /*加在popu的布局*/
    private View mPopView;
    /*popupWindow*/
    private PopupWindow mPopupWindow;
    /*PopupWindow 中的三个控件*/
    private TextView app_cancle;
    private TextView app_exit;
    private TextView app_change;

    @InjectInit
    private void init() {
        mContext = this;
        mPopView = LayoutInflater.from(this).inflate(R.layout.popu_app_exit, null);
        app_cancle = (TextView) mPopView.findViewById(R.id.app_cancle);
        app_change = (TextView) mPopView.findViewById(R.id.app_change_user);
        app_exit = (TextView) mPopView.findViewById(R.id.app_exit);

        mPopupWindow = new PopupWindow(mPopView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        /*取消PopupWindows*/
        app_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        /*注销用户登陆*/
        app_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.activity_up, R.anim.fade_out);
                finish();
            }
        });
        /*一键退出程序*/
        app_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().AppExit(BaseApp.getInstance());
            }
        });


        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        NewsFatherFragment newsFatherFragment = new NewsFatherFragment();
        ft.replace(R.id.fragment_container, newsFatherFragment);
        ft.commit();

    }

    /**
     * 弹出菜单选项
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#b0000000")));
            mPopupWindow.showAtLocation(main_rgs, Gravity.BOTTOM, 0, 0);
            mPopupWindow.setAnimationStyle(R.style.app_pop);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setFocusable(true);
            mPopupWindow.update();
        }
        return super.onKeyDown(keyCode, event);

    }
}
